/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.UseCase;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagram.usecase.helper.NodeUseCaseHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Property;

// TODO: Auto-generated Javadoc
/**
 * The Class UseCasePresentationSection.
 * 
 * @author Gilles Cannenterre
 * @since 14 nov. 06
 */
public class UseCasePresentationSection extends PropertyViewSection {


	/**
	 * Listener for a direct edit manager.
	 * 
	 * @author Remi SCHNEKENBURGER
	 */
	protected class DirectEditSelectionListener implements org.eclipse.swt.events.SelectionListener {
		
		/**
		 * The use case presentation.
		 */
		private String useCasePresentation;
		
		/**
		 * The Constructor.
		 * 
		 * @param actorPresentation the actor presentation
		 */
		DirectEditSelectionListener(String actorPresentation) {
			this.useCasePresentation = actorPresentation;
		}
		
		/**
		 * Widget selected.
		 * 
		 * @param e the e
		 */
		public void widgetSelected(SelectionEvent e) {
			if(getElement() instanceof UseCase) {
				setCurrentAppearance(useCasePresentation);
				if (diagramElement != null){
					if (!(diagramElement.getProperty().isEmpty())){
						Property useCaseProperty = (Property) diagramElement.getProperty(NodeHelper.NODE_FIGURE_APPEARANCE);
						if (useCaseProperty != null){
							useCaseProperty.setValue(useCasePresentation);
							refresh();
						}
					}
				}		
			}
		}

		/**
		 * Widget default selected.
		 * 
		 * @param e the e
		 */
		public void widgetDefaultSelected(SelectionEvent e){}
	}

	/**
	 * The check box ellipse form.
	 */
	private Button checkBoxEllipseForm;
	
	/**
	 * The check box ellipse form name outside.
	 */
	private Button checkBoxEllipseFormNameOutside;
	
	/**
	 * The check box classifier form.
	 */
	private Button checkBoxClassifierForm ;
		
	/**
	 * The ellipse form selection listener.
	 */
	private DirectEditSelectionListener ellipseFormSelectionListener = new DirectEditSelectionListener(NodeUseCaseHelper.USECASE_ELLIPSE_FORM);
	
	/**
	 * The ellipse form name outside selection listener.
	 */
	private DirectEditSelectionListener ellipseFormNameOutsideSelectionListener = new DirectEditSelectionListener(NodeUseCaseHelper.USECASE_ELLIPSE_FORM_NAME_OUTSIDE);

	/**
	 * The classifier form selection listener.
	 */
	private DirectEditSelectionListener classifierFormSelectionListener = new DirectEditSelectionListener(NodeUseCaseHelper.USECASE_CLASSIFIER_FORM);

	/**
	 * The current appearance.
	 */
	private String currentAppearance;
	
	/**
	 * Creates the controls.
	 * 
	 * @param tabbedPropertySheetPage the tabbed property sheet page
	 * @param parent the parent
	 */
	public void createControls(final Composite parent,
			final TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		
		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
		
		// Ellipse form
		checkBoxEllipseForm = this.getWidgetFactory().createButton(composite, "Ellipse Form", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		checkBoxEllipseForm.setLayoutData(data);
		checkBoxEllipseForm.addSelectionListener(ellipseFormSelectionListener);
		
		// Ellipse form name outside
		checkBoxEllipseFormNameOutside = this.getWidgetFactory().createButton(composite, "Ellipse Form Name Outside", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxEllipseForm, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxEllipseFormNameOutside.setLayoutData(data);
		checkBoxEllipseFormNameOutside.addSelectionListener(ellipseFormNameOutsideSelectionListener);
		
		// Classifier form
		checkBoxClassifierForm = this.getWidgetFactory().createButton(composite, "Classifier Form", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxEllipseFormNameOutside, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxClassifierForm.setLayoutData(data);
		checkBoxClassifierForm.addSelectionListener(classifierFormSelectionListener);	
		
		// Label of the section
		CLabel useCasePresentationLabel = this.getWidgetFactory().createCLabel(composite, "Presentation:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(checkBoxEllipseForm,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(checkBoxEllipseForm, 0, SWT.CENTER);
		useCasePresentationLabel.setLayoutData(data);
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * Refresh the view othe multicplicity in the multicplicityCombo.
	 */
	@Override
	public void refresh() {
		if(!checkBoxEllipseForm.isDisposed() && !checkBoxClassifierForm.isDisposed()){
			// remove all listeners
			checkBoxEllipseForm.removeSelectionListener(ellipseFormSelectionListener);
			checkBoxEllipseFormNameOutside.removeSelectionListener(ellipseFormNameOutsideSelectionListener);
			checkBoxClassifierForm.removeSelectionListener(classifierFormSelectionListener);
		
			if(getElement() instanceof UseCase) {
				if (diagramElement != null){
					
					if (((GraphElement)diagramElement).getSemanticModel() != null ){
						checkBoxEllipseForm.setEnabled(true);	
						checkBoxEllipseFormNameOutside.setEnabled(true);
						checkBoxClassifierForm.setEnabled(true);
					
						if (!(diagramElement.getProperty().isEmpty())){
							Property useCaseProperty = (Property) diagramElement.getProperty(NodeHelper.NODE_FIGURE_APPEARANCE);
							if (useCaseProperty != null){
								currentAppearance = useCaseProperty.getValue();
							}
						}
						
					}else {
						checkBoxEllipseForm.setEnabled(false);	
						checkBoxEllipseFormNameOutside.setEnabled(false);
						checkBoxClassifierForm.setEnabled(false);
					}
					
				}				
			}
		
			checkBoxEllipseForm.setSelection(NodeUseCaseHelper.USECASE_ELLIPSE_FORM.equals(currentAppearance));
			checkBoxEllipseFormNameOutside.setSelection(NodeUseCaseHelper.USECASE_ELLIPSE_FORM_NAME_OUTSIDE.equals(currentAppearance));
			checkBoxClassifierForm.setSelection(NodeUseCaseHelper.USECASE_CLASSIFIER_FORM.equals(currentAppearance));
		
			checkBoxEllipseForm.addSelectionListener(ellipseFormSelectionListener);
			checkBoxEllipseFormNameOutside.addSelectionListener(ellipseFormNameOutsideSelectionListener);
			checkBoxClassifierForm.addSelectionListener(classifierFormSelectionListener);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(checkBoxEllipseForm!=null && checkBoxClassifierForm!=null && checkBoxEllipseFormNameOutside!=null &&
				!checkBoxEllipseForm.isDisposed() && !checkBoxClassifierForm.isDisposed() && !checkBoxEllipseFormNameOutside.isDisposed()){
			checkBoxEllipseForm.removeSelectionListener(ellipseFormSelectionListener);
			checkBoxEllipseFormNameOutside.removeSelectionListener(ellipseFormNameOutsideSelectionListener);
			checkBoxClassifierForm.removeSelectionListener(classifierFormSelectionListener);
		}
	}
	

	/**
	 * Gets the current appearance.
	 * 
	 * @return the current appearance
	 */
	public String getCurrentAppearance() {
		return currentAppearance;
	}

	/**
	 * Sets the current appearance.
	 * 
	 * @param currentVisibility the currentVisibility to set
	 * @param currentAppearance the current appearance
	 */
	public void setCurrentAppearance(String currentAppearance) {
		this.currentAppearance = currentAppearance;
	}
}
